import coreFunc from "../core/func"
import Cart from "./module/Cart"
import platformType from "../app/type/PlatformType"
import UserInfo from "../app/module/UserInfo"
import Order from "../app/module/Order"
import Magappx from "../app/platform/Magappx"
import Web from "../app/platform/Web"
import Ejia from "../app/platform/Ejia"

export default {
  // region cartgoods
  addCartGoods(cartGoods, want) {
    let cart = this.getCart();
    if (cart.goodsList.length <= 0) {
      let price = (Number(cartGoods.realPrice) * Number(cartGoods.count)).toFixed(2)
      cartGoods.cartGoodsPrice = price;
      cart.goodsList.push({cartGoods: cartGoods, want: want});
      coreFunc.setLocalStrange("cart", cart)
    } else {
      let yanxuanIds = [];
      let skuIds = [];
      let added = false;
      cart.goodsList.forEach(_cartGoods => {
        if (!coreFunc.listHas(yanxuanIds, _cartGoods.cartGoods.yanxuanId))
          yanxuanIds.push(_cartGoods.cartGoods.yanxuanId);
        if (!coreFunc.listHas(skuIds, _cartGoods.cartGoods.skuId))
          skuIds.push(_cartGoods.cartGoods.skuId);

        if (_cartGoods.cartGoods.yanxuanId === cartGoods.yanxuanId && _cartGoods.cartGoods.skuId === cartGoods.skuId) {
          _cartGoods.cartGoods.count += Number(cartGoods.count);
          let price = Number(_cartGoods.cartGoods.count) * Number(_cartGoods.cartGoods.realPrice);
          _cartGoods.cartGoods.cartGoodsPrice = Number(price).toFixed(2);
          coreFunc.setLocalStrange("cart", cart);
          added = true;
        }
      });
      if (!added) {
        if (coreFunc.listHas(yanxuanIds, cartGoods.yanxuanId) && !coreFunc.listHas(skuIds, cartGoods.skuId)) {
          let cart = this.getCart();
          let price = (Number(cartGoods.realPrice) * Number(cartGoods.count)).toFixed(2)
          cartGoods.cartGoodsPrice = price;
          let data = {cartGoods: cartGoods, want: want};
          cart.goodsList.push(data);
          coreFunc.setLocalStrange("cart", cart);
          this.sumCart();
          added = true;
          return;
        }
      }
      if (!added) {
        let cart = this.getCart();
        let price = (Number(cartGoods.realPrice) * Number(cartGoods.count)).toFixed(2)
        cartGoods.cartGoodsPrice = price;
        cart.goodsList.push({cartGoods: cartGoods, "want": want});
        coreFunc.setLocalStrange("cart", cart);
        this.sumCart();
        added = true;
      }
    }
    this.sumCart();
  },

  sumCart() {
    let cart = this.getCart();
    let price = 0;
    cart.goodsList.forEach(cartGoods => {
      if (cartGoods.want) {
        price += Number(cartGoods.cartGoods.cartGoodsPrice)
      }
    });
    cart.cartPrice = Number(price).toFixed(2);
    coreFunc.setLocalStrange("cart", cart)
  },

  getCart() {
    return coreFunc.getLocalStrange("cart")
  },

  getCartList() {
    return coreFunc.getLocalStrange("cart").goodsList
  },

  getWantCartGoodsList() {
    let cartGoodsList = this.getCartList();
    let list = [];
    cartGoodsList.forEach(cartgoods => {
      if (cartgoods.want) {
        list.push(cartgoods.cartGoods)
      }
    });
    return list;
  },

  getWantCartGoodsCount() {
    let cartGoodsList = this.getCartList();
    let count = 0;
    cartGoodsList.forEach(cartgoods => {
      if (cartgoods.want) {
        count += Number(cartgoods.cartGoods.count)
      }
    });
    return count;
  },

  clearCart() {
    coreFunc.setLocalStrange("cart", Cart())
  },

  changeCartGoodsWant(indexCartGoods, want) {
    let cart = this.getCart();
    cart.goodsList[indexCartGoods].want = want;
    coreFunc.setLocalStrange("cart", cart);
    this.sumCart()
  },

  removeCartGoods(indexCartGoods) {
    let cart = this.getCart();
    cart.goodsList.splice(indexCartGoods, 1);
    coreFunc.setLocalStrange("cart", cart);
    this.sumCart()
  },

  changeCartGoodsCount(indexCartGoods, count) {
    let cart = this.getCart();
    cart.goodsList[indexCartGoods].cartGoods.count = count;
    cart.goodsList[indexCartGoods].cartGoods.cartGoodsPrice = (Number(cart.goodsList[indexCartGoods].cartGoods.realPrice) * Number(count)).toFixed(2);
    coreFunc.setLocalStrange("cart", cart);
    this.sumCart()
  },
  // endregion

  //region Order
  createOrder(cartGoodsList, price, count, address) {
    let order = Order(cartGoodsList, price, count, address);
    coreFunc.setLocalStrange("order", order)
  },
  getOrder() {
    return coreFunc.getLocalStrange("order")
  },
  //endregion

  // region cartgoodsCache
  setGoodsSizeCache(cartGoods) {
    coreFunc.setLocalStrange("CacheGoodsSize", cartGoods);
  },

  getGoodsSizeCache() {
    return coreFunc.getLocalStrange("CacheGoodsSize");
  },
  clearGoodsSizeCache() {
    coreFunc.setLocalStrange("CacheGoodsSize", {});
  },
  // endregion

  // region platformCache
  setPlatform() {
    let userAgent = coreFunc.userAgent();
    if (userAgent.indexOf("MAGAPPX") !== -1) {
      coreFunc.setLocalStrange("platform", platformType.MAGAPPX)
    } else {
      coreFunc.setLocalStrange("platform", platformType.WEB)
    }
    coreFunc.setLocalStrange("platform", platformType.MAGAPPX)
  },
  getPlatform() {
    return coreFunc.getLocalStrange("platform")
  },

  login() {
    let platform = this.getPlatform();
    switch (platform) {
      case platformType.MAGAPPX:
        Magappx.login();
        break;
      case platformType.WEB:
        Web.login();
        break;
      case platformType.EJIA:
        Ejia.login();
        break;
      default:
        Web.login();
        break;
    }
  },
  // endregion

  // region userinfoCache
  setUserinfoCache(userId, userPhone, userName, userAvatar = "//yanxuan.nosdn.127.net/8945ae63d940cc42406c3f67019c5cb6.png") {
    coreFunc.setLocalStrange("user", UserInfo(userId, userPhone, userName, userAvatar, this.getPlatform()))
  },
  getUserinfoCache() {
    return coreFunc.getLocalStrange("user")
  },
  clearUserInfoCache() {
    coreFunc.setLocalStrange("user", {})
  },
  // endregion

  // region addressCache
  setAddress(address) {
    coreFunc.setLocalStrange("address", address)
  },
  getAddress() {
    return coreFunc.getLocalStrange("address")
  },
  clearAddess() {
    coreFunc.setLocalStrange("address", {})
  },
  // endregion

  // region ActiveMenu
  getActiveNav() {
    return coreFunc.getLocalStrange("activeNav")
  },
  setActiveNav(id) {
    coreFunc.setLocalStrange("activeNav", id)
  },
  // endregion

  // region other
  isLogin() {
    if (coreFunc.emptyObj(this.getUserinfoCache())) {
      switch (this.getPlatform()) {
        case platformType.MAPAPP:
          window.location.href = "/#/NoUser";
          break;
        case platformType.WEB:
          window.location.href = "/#/Login";
          break;
        case platformType.EJIA:
          window.location.href = "/#/NoUser";
          break;
        default:
          window.location.href = "/#/NoUser";
          break;
      }
    }
  },
  platformPay(orderId) {
    switch (this.getPlatform()) {
      case platformType.MAGAPPX:
        window.location.href = '/#/MagappPay/?id=' + orderId;
        break;
      case platformType.WEB:
        break;
      case platformType.EJIA:
        break;
      default:
        console.log('没平台，交你妹的钱啊！');
        break;
    }
  },
  clearPlatForm(platformType) {
    coreFunc.setLocalStrange("platform", platformType)
  },
  // endregion
}
